package org.shop.controller.pay;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.shop.controller.pay.util.XMLUtil;

/**
 * @author zhongp
 * @version 1.0.1
 * @data 2017/11/8
 */
public class WxHttpClientUtils {

    /**
     * 微信支付请求
     * @param url 请求url
     * @param xml 传入参数
     * @return
     */
    public static String reqPost(String url, String xml) {
        CloseableHttpClient httpclient = HttpClientManager.getHttpClient();
        CloseableHttpResponse response=null;
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.addHeader("Content-type", "text/xml; charset=utf-8");
            httpPost.setEntity(new StringEntity(xml, Charset.forName("UTF-8")));
            response = httpclient.execute(httpPost);
            //执行请求
            String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");
            EntityUtils.consume(response.getEntity());
            return responseEntity;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(ObjectUtils.isNotEmpty(httpPost)){
                httpPost.releaseConnection();
            }
        }
        return null;
    }
    /**
     * 微信提现请求
     * @param url 请求url
     * @param xml 传入参数
     * @return
     */
    public static String reqWithdrawalPost(String url, String xml) {
    	SSLConnectionSocketFactory sslsf = null;
		try {
			sslsf = CertUtil.initCert();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    	CloseableHttpResponse response=null;
    	HttpPost httpPost = new HttpPost(url);
    	try {
    		httpPost.addHeader("Content-type", "text/xml; charset=utf-8");
    		httpPost.setEntity(new StringEntity(xml, Charset.forName("UTF-8")));
    		response = httpclient.execute(httpPost);
    		//执行请求
    		String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");
    		EntityUtils.consume(response.getEntity());
    		return responseEntity;
    	} catch (IOException e) {
    		e.printStackTrace();
    	}finally {
    		if(ObjectUtils.isNotEmpty(httpPost)){
    			httpPost.releaseConnection();
    		}
    	}
    	return null;
    }
    
    /**
     * 查询微信订单
     * @param url
     * @param xmlParam
     * @return
     */
    public static Map<String,String> queryWxDoXml(String url, String xmlParam) {
        CloseableHttpClient httpclient = HttpClientManager.getHttpClient();
        HttpPost httpPost = new HttpPost(url);
		Map<String,String> map =new HashMap<String,String>();
		try {
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			HttpResponse response1 = httpclient.execute(httpPost);
			String jsonStr = EntityUtils
					.toString(response1.getEntity(), "UTF-8");
			System.out.println(jsonStr);
			map = XMLUtil.doXMLParse(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return map;
	}

}
